package luj.tool.jnproxyan.proxy.network.socks.actor.root.message.http.client2p.response.write;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpObject;
import luj.tool.jnproxyan.proxy.network.logger.Client2pLogger;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor;
import luj.tool.jnproxyan.proxy.network.socks.request.SocksRequest;
import luj.tool.jnproxyan.proxy.network.socks.request.op.RequestByIdGetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
final class OnC2pRspWrite implements Socks5Actor.Handler<C2pRspWriteMsg> {

  @Override
  public void onHandle(Context ctx) throws Exception {
    Socks5Actor self = ctx.getActorState(this);
    C2pRspWriteMsg msg = ctx.getMessage(this);

    Long socksId = msg.socksId();
    SocksRequest socks = _reqGetter.inject(self, socksId).getReq();

    HttpObject data = msg.data();
    new Client2pLogger(LOG, socksId).log2c("发送响应：{}", data.getClass().getName());

    Channel c2pConn = socks.getC2pConnV2();
    c2pConn.writeAndFlush(data);
  }

  private static final Logger LOG = LoggerFactory.getLogger(OnC2pRspWrite.class);

  @Autowired
  RequestByIdGetter _reqGetter;
}
